package org.example.过滤消息.生产;

import lombok.SneakyThrows;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

/**
 * @Auther:Liu
 * @Testname:过滤消息发送
 * @Date:2025/8/22 18:52
 */
public class 过滤消息发送 {
    @SneakyThrows
    public static void main(String[] args) {
        DefaultMQProducer producer = new DefaultMQProducer("producer_group_b");
        producer.setNamesrvAddr("192.168.10.88:9876");
        producer.start();
        for (int i = 0; i < 10; i++) {
            int age=(int)(Math.random()*100+1);
            char gender = ((int) (Math.random() * 100 + 1)) % 2 == 0 ? '男' : '女';
            String topic = "TopicA";
            String body = "Hello,Guolv!index:" + i + ",age:" + age + ",gender:" + gender;
            Message message = new Message(topic, "", body.getBytes(StandardCharsets.UTF_8));
            // 设置过滤条件
            message.putUserProperty("age", age + "");
            message.putUserProperty("gender", gender + "");
            producer.send(message);
        }
        producer.shutdown();
    }
}